var url = document.location.toString();
var arrUrl = url.split("//");
var start = arrUrl[1].split("/")[1];

var he = HE.getEditor('editor', {
    html:'1',
    width: '100%',
    height: '300px',
    autoHeight: true,
    autoFloat: false,
    topOffset: 0,
    lang: 'zh-jian',
    skin: 'HandyEditor',
    externalSkin: '',
    item: ['italic','fontName', 'color', 'left','expression',   ]
});

$(function () {
    pl();
    //提交评论
    $(".btn_tj").on("click", function () {
        $.get("/is/login", function (result) {
            if (result.code == 400) {
                var email = $("#text_email").val();
                if (email != null && email != undefined && email != "") {
                    sessionStorage.setItem("email", email);
                }
                document.cookie="redirect_url="+start;
                layer.open({
                    btn: '',
                    offset: '36%',
                    title: '登陆',
                    content: '<span id="msg" style="color: red;"></span><br><div ><label>用户名：</label><input type="text" id="username"  style="width: 160px"></div><div style="margin-top: 8px"><label>密&nbsp&nbsp&nbsp&nbsp码：<input type="password" id="password"  style="width: 160px"></label></div>' +
                        '<div style="margin-top: 8px"><a href="/login/qq"><img src="/img/qq_logo_7.png"></a><input type="submit" value="登陆" onclick="login()" style="margin-left: 90px; display: inline-block;height: 28px;line-height: 28px;padding: 0 18px;background-color: #1E9FFF;color: #fff;white-space: nowrap;text-align: center;font-size: 14px;border: none;border-radius: 2px;cursor: pointer;" ></div>'

                });
            } else {
                var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
                if (he.getText().length >0) {
                    $.ajax({
                        type: 'POST',
                        url: '/api/addmessage',
                        dataType:"json",
                        data: {
                            'context': he.getHtml(),
                            'email': $(".email").val()
                        },
                        success: function (result) {
                            if (result.code==200){
                               $(".HandyEditor_editor").html("")
                                layer.msg('留言成功！', {icon: 1, offset: '36%'});
                                pl();
                            } else {
                                layer.msg(result.message, {icon: 5, offset: '36%'});
                            }
                        }
                    });
                } else {
                    layer.msg('请填写留言内容！', {offset: '36%'});
                }

            }
        })
    });


})

function login() {
    $.ajax({
        url: '/dologin',
        type: 'POST',
        data: "username=" + $("#username").val() + "&password=" + $("#password").val(),
        success: function () {
            $.get("/is/login", function (result) {
                if (result.code == 400) {
                    $("#msg").html("用户名或密码错误！");
                } else {
                    loadUserInfo();
                    document.cookie="redirect_url=";
                    layer.alert('&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp登陆成功！', {icon: 1, offset: '260px'});
                }
            })
        },
        error: function () {
            $("#msg").html("用户名或密码错误！");
        }
    });
}



function pl() {
    $.ajax({
        url: "/api/message",
        type: "get",
        dataType: "json",
        beforeSend: function () {
            $(".message_list").html("");
        },
        success: function (result) {
            var msg =result.data
            var str = "";
            var data = result.data;
            if (data == null || data.length < 1) {
                $(".message_em").text("0")
                $(".message_list").html("当前没有任何评论...")
            } else {
                $(".message_em").text("" + result.totalSize + "")
                var photo = ""
                for (var i = 0; i < msg.length; i++) {
                    str = "<li class='message_li'><div class='message_liuyan clear' id='" + msg[i].id + "_'><div class='liuyan_img'><img src='" + msg[i].icon + "' /></div><div class='liuyan_con'><div class='liuyan_name'>" + msg[i].nickname + "</div><div class='liuyan_con'>" + msg[i].context + "</div> <div class='liuyan_time'><span>" + msg[i].createTime + "</span><a role='button' class='btn_mess_hf'  id='" + msg[i].id + "'>回复</a></div></div></div><div class='message_txt clear'><textarea class='message_textarea'></textarea><a role='button' class='btn_mess_tj'>提交</a><a role='button' class='btn_mess_sq'>收起</a><label class='hf_ts'></label></div></li>";
                    $(".message_list").append(str);
                    str=""
                    if (msg[i].child != null) {
                        var sto = "";
                        for (var j = 0; j < msg[i].child.length; j++) {
                            sto += "<div class='message_reply clear' id='" + msg[i].child[j].id + "'><div class='reply_img'><img src='" + msg[i].child[j].icon + "' /></div><div class='reply_con'><div class='reply_name'>" + msg[i].child[j].nickname + "</div><div class='reply_con'>" + msg[i].child[j].context + "</div><div class='reply_time'><span>" + msg[i].child[j].createTime + "</span></div></div></div>"
                        }
                        ;$("#" + msg[i].id + "_").after(sto)
                    }
                    ;$('.btn_mess_hf').unbind('click');
                    $(".btn_mess_hf").click(function () {
                        $(this).parent().parent().parent().siblings(".message_txt").css({"display": "block"});
                        var a = $(this).attr("id");
                        $('.btn_mess_tj').unbind('click');
                        $(".btn_mess_tj").click(function () {
                            var tare = $(this).prev(".message_textarea").val();
                        $.get("/is/login", function (result) {
                            if (result.code==400) {
                                layer.open({
                                    btn: '',
                                    offset: '36%',
                                    title: '登陆',
                                    content: '<span id="msg" style="color: red;"></span><br><div ><label>用户名：</label><input type="text" id="username"  style="width: 160px"></div><div style="margin-top: 8px"><label>密&nbsp&nbsp&nbsp&nbsp码：<input type="password" id="password"  style="width: 160px"></label></div>' +
                                        '<div style="margin-top: 8px"><a href="/login/qq"><img src="/img/qq_logo_7.png"></a><input type="submit" value="登陆" onclick="login()" style="margin-left: 90px; display: inline-block;height: 28px;line-height: 28px;padding: 0 18px;background-color: #1E9FFF;color: #fff;white-space: nowrap;text-align: center;font-size: 14px;border: none;border-radius: 2px;cursor: pointer;" ></div>'

                                });
                            } else {
                                $.ajax({
                                    type: 'POST',
                                    url: '/api/addmessage',
                                    dataType: "json",
                                    data: {
                                        'writeBack': a,
                                        'context':tare,
                                        'email': $(".email_2").val()
                                    },
                                    success: function (result) {
                                        if (result.code==200){
                                            layer.msg('回复成功！', {icon: 1, offset: '36%'});
                                            pl()
                                        } else {
                                            layer.msg(result.message, {icon: 5, offset: '36%'});
                                        }
                                    }
                                });
                                $(".message_txt").css({"display": "none"})
                            }
                        })

                        })
                        $(".btn_mess_sq").click(function () {
                            $(".message_txt").css({"display": "none"})
                        })
                    })
                }
                if (result.totalSize == 0) {
                    $(".message_list").css({
                        "display": "none"
                    })
                } else {
                    $(".message_list").css({
                        "display": "block"
                    })
                }
            }
        },
        error: function (err) {
            // layer.msg("服务繁忙，请稍后再试！",{icon: 1});
        },
        complete: function () {
        }
    });
}
